package com.cpi.cornpp.plants.action;

import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.collections.CollectionUtils;
import org.apache.struts.Globals;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.plants.exception.PlantException;
import com.cpi.cornpp.plants.form.PlantForm;
import com.cpi.cornpp.plants.services.PlantService;
import com.cpi.cornpp.purchase.program.services.PurchaseProgramService;

/**
 * Plant Delete Action
 * @author Edgar Joao
 *
 */
public class PlantDeleteAction extends Action{

	/** Error message name*/
	private final static String ERROR_MESSAGE = "ERROR_MESSAGE";//$NON-NLS-1$
	/** Custom exception */
	private final static String CUSTOM_EXCEPTION = "custom_exception";//$NON-NLS-1$
	/** Fail forward */
	private final static String FAIL_FORWARD = "fail";//$NON-NLS-1$
	/** Success forward */
	private final static String SUCCESS_FORWARD = "success";//$NON-NLS-1$
	/** Information message name*/
	private final static String MESSAGE = "MESSAGE";//$NON-NLS-1$
	/** Success delete key */
	private static final String SUCCESS_DELETE_LABEL = "common.delete.success";//$NON-NLS-1$
	private static final String PLANTS_PURCHASE_ORDER_RELATED_MESSAGE = "info.message.plants.delete.relatedPurchaseOrders";//$NON-NLS-1$
	/*
	 * (non-Javadoc)
	 * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws PlantException {
		
		PlantService service = null;
		PurchaseProgramService purchaseProgramService = null;
		MessageResources messageResources = null;
		String messageError = null;				
		PlantForm plantForm;
		String forward = null;
		List plantPurchaseOrders = null;
		HttpSession httpSession = null;
		plantForm = (PlantForm) form;
		String plantId = null;
		try{
			httpSession = request.getSession(false);
			messageResources = getResources(request);
		
		if(plantForm != null){
			service = new PlantService();
			purchaseProgramService = new PurchaseProgramService();
			
			plantId = request.getParameter("plantId");
			
			plantPurchaseOrders = purchaseProgramService.getPurchaseOrdersByPlant(Integer.parseInt(plantId));
			
			if (CollectionUtils.isEmpty(plantPurchaseOrders)){
				plantForm.getPlantView().setPlantId(Integer.parseInt(plantId));
				service.deletePlant(plantForm.getPlantView());
				messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), SUCCESS_DELETE_LABEL);
			} else {
				messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), PLANTS_PURCHASE_ORDER_RELATED_MESSAGE);
			}
			
			httpSession.setAttribute(MESSAGE, messageError);
			forward = SUCCESS_FORWARD;	
			
		}
		
		}catch (PlantException plantException){
			messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), plantException.getErrorCode());
			if(null == messageError){
				httpSession.setAttribute(CUSTOM_EXCEPTION, plantException);
				form.reset(mapping, request);
				throw plantException;
			}else{
				httpSession.setAttribute(ERROR_MESSAGE, messageError);
				forward = FAIL_FORWARD;
			}
			throw plantException;
		}
		catch (Exception e){
			PlantException plantException=null;
			e.printStackTrace(System.out);
			plantException= new PlantException(e , CornPPException.LAYER_ACTION,CornPPException.ACTION_DELETE);
			throw plantException;
		}
		form.reset(mapping, request);				
		return mapping.findForward(forward);
	}
}
